package com.vf.admin.platform.controller;

import com.google.zxing.common.BitMatrix;
import com.vf.admin.utils.QRcodeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * 二维码调用前端控制器
 */
@RequestMapping("/Qrcode")
@RestController
public class QrcodeController {

    public static void main(String[] args){
        QrcodeController qrcode = new QrcodeController();
        qrcode.productcode();
    }
    /**
     * 生成二维码
     */
    public void productcode() {
        QRcodeUtils qrCode = new QRcodeUtils();
        String content = "http://www.baidu.com";
        String logoPath = "D:/erweima/logo.jpg";
        String format = "jpg";
        int width = 180;
        int height = 220;
        BitMatrix bitMatrix = qrCode.setBitMatrix(content, width, height);
        //可通过输出流输出到页面,也可直接保存到文件
        OutputStream outStream = null;
        String path = "D:/erweima/22.png";
        try {
            outStream = new FileOutputStream(new File(path));
            qrCode.writeToFile(bitMatrix, format, outStream, logoPath);
            outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 添加文字效果
        int fontSize = 12; // 字体大小
        int fontStyle = 1; // 字体风格
        String text = "烽火任务平台";
        String withTextPath = "D:/erweima/33.png";
        qrCode.pressText(text, withTextPath, path, fontStyle, Color.RED, fontSize, width, height);
//        qrCode.zxingCodeCreate("http://www.baidu.com", "D:/erweima/",500,"D:/erweima/111.jpg");
    }


    /**
     * 解析二维码
     */
    @GetMapping("/jiexi")
    public void analysiscode() {
        QRcodeUtils qrCode = new QRcodeUtils();
        String content = qrCode.decodeQR("D:/erweima/111.png");
        System.out.println("content:"+content);
//        Result result = new QRCodeUtil().zxingCodeAnalyze("D:/erweima/789.jpg");
//        System.out.println("对象:"+result);
//        System.err.println("二维码解析内容："+result.toString());
    }

}